#include "game.h"
#include "Allegro_initialize.h"

Game::Game (vector<vector<int> > &_level_data)
{
	init_game(_level_data);
}

Game::Game ()
{
	init_game();
}

void Game::init_game()
{
	_dig_dug = Digdug(_game_grid_elements/2, _game_grid_elements/2, 1, 1);
//	_monster = Monster;
	_screen_map = generate_default_map();
}

void Game::init_game(vector<vector<int> > &_level_data)
{
	//
	//Level Data contains our objects, stored 
	//The first thign we want to do is generate our objects from the provided data
	//to populate our map from our
	//Preloaded map data.
	
	

	//Now we want to create our movable objects
	
}

void Game::update(Direction dir)
{
	//If keyboard input != shoot
	//Change the direction of digdug
	//then call querymove and pass in digdug
	//
	if(dir !=NONE)
	{
	move_Digdug(_dig_dug, dir);
	}

}


vector<vector<Movable_type> > Game::generate_default_map()
{
	vector<Movable_type> _line_map;
	vector<vector<Movable_type> > _lines_map;
	for(int i =0; i < _game_grid_elements; i++)
	{
		_line_map.clear();

		for(int j = 0; j < _game_grid_elements; j++)
		{
			if((i>=0.3*_game_grid_elements) && (i<0.6*_game_grid_elements) && (j>=0.3*_game_grid_elements) && (j<0.6*_game_grid_elements))
			{
				_line_map.push_back(HOLE);
			}
			else
			{
				_line_map.push_back(SAND);
			}

		}
		_lines_map.push_back(_line_map);
	}

	return _lines_map;
}

 bool Game::is_sand_at (int i, int j)
 {
	 if(_screen_map[i][j] == SAND)
	 {
		 return true;
	 }
	 return false;
 }

 int Game::get_number_of_objects()
 {
	 return 1;
 }

int Game::get_object_x(int object_count)
{
	return _dig_dug.get_x_location();
}
int Game::get_object_y(int object_count)
{
	return _dig_dug.get_y_location();
}
int Game::get_object_border(int object_count)
{
	return _dig_dug.get_x_boundary();
}
ALLEGRO_BITMAP * Game::get_object_bitmap(int object_count)
{
	return _dig_dug.get_bitmap();
}
//----------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------
void Game::move_digdug_up( Digdug &movable, Move_status &final_move_status)
{
	// return the x-location of the center of the object
	int start_movable_center_x = movable.get_x_location(); 
// return the y-location of the center of the object
	int start_movable_center_y = movable.get_y_location();
// get the coordinates and dimensions of the movable
	int start_top_of_movable = start_movable_center_y - movable.get_y_boundary();
	int start_bottom_of_movable = start_movable_center_y + movable.get_y_boundary();
	int start_LHS_of_movable = start_movable_center_x - movable.get_x_boundary();
	int start_RHS_of_movable = start_movable_center_x + movable.get_x_boundary();
	int height_of_movable = 2*movable.get_y_boundary() + 1;
	int speed_of_movable = movable.get_speed();
	int end_top_of_movable = start_top_of_movable  - speed_of_movable;

	for (int j = start_top_of_movable - 1; j >= end_top_of_movable  && j >= 0; j--)
			{
				for (int i = start_LHS_of_movable; i <= start_RHS_of_movable; i++)
				{
					if( _screen_map[i][j] == SAND || _screen_map[i][j] == HOLE)
					{					
						_screen_map[i][j] = DIGDUG;
						_screen_map[i][j + height_of_movable] = HOLE;
						movable.set_location(start_movable_center_x, j + movable.get_y_boundary());

					}

					//else if( _screen_map[i][j] == HOLE)
					//{					
					//	_screen_map[i][j] = DIGDUG;
					//	_screen_map[i][j + height_of_movable +1] = HOLE;
					//}

					else if(_screen_map[i][j] == MONSTER)
					{
						movable.set_status(DEAD);
						final_move_status = DIED_TRYING_TO_MOVE;
					}
				}
			}
}
	
void Game::move_digdug_down( Digdug &movable, Move_status &final_move_status)
{
	// return the x-location of the center of the object
	int start_movable_center_x = movable.get_x_location(); 
// return the y-location of the center of the object
	int start_movable_center_y = movable.get_y_location();
// get the coordinates and dimensions of the movable
	int start_top_of_movable = start_movable_center_y - movable.get_y_boundary();
	int start_bottom_of_movable = start_movable_center_y + movable.get_y_boundary();
	int start_LHS_of_movable = start_movable_center_x - movable.get_x_boundary();
	int start_RHS_of_movable = start_movable_center_x + movable.get_x_boundary();
	int height_of_movable = 2*movable.get_y_boundary() + 1;
	int speed_of_movable = movable.get_speed();

	int end_bottom_of_movable = start_bottom_of_movable  + speed_of_movable;

	for (int j = start_bottom_of_movable + 1; j <= end_bottom_of_movable  && j < _game_grid_elements; j++)
			{
				for (int i = start_LHS_of_movable; i <= start_RHS_of_movable; i++)
				{
					if( _screen_map[i][j] == SAND || _screen_map[i][j] == HOLE)
					{					
						_screen_map[i][j] = DIGDUG;
						_screen_map[i][j - height_of_movable ] = HOLE;
						movable.set_location(start_movable_center_x, j - movable.get_y_boundary());
					}

					//else if( _screen_map[i][j] == HOLE)
					//{					
					//	_screen_map[i][j] = DIGDUG;
					//	_screen_map[i][j + height_of_movable +1] = HOLE;
					//}

					else if(_screen_map[i][j] == MONSTER)
					{
						movable.set_status(DEAD);
						final_move_status = DIED_TRYING_TO_MOVE;
					}
				}
			}
}
	
void Game::move_digdug_left( Digdug &movable, Move_status &final_move_status)
{
	// return the x-location of the center of the object
	int start_movable_center_x = movable.get_x_location(); 
// return the y-location of the center of the object
	int start_movable_center_y = movable.get_y_location();
// get the coordinates and dimensions of the movable
	int start_top_of_movable = start_movable_center_y - movable.get_y_boundary();
	int start_bottom_of_movable = start_movable_center_y + movable.get_y_boundary();
	int start_LHS_of_movable = start_movable_center_x - movable.get_x_boundary();
	int start_RHS_of_movable = start_movable_center_x + movable.get_x_boundary();
	int length_of_movable = 2*movable.get_x_boundary() + 1;
	int speed_of_movable = movable.get_speed();

	int end_LHS_of_movable = start_LHS_of_movable  - speed_of_movable;

	for (int i = start_LHS_of_movable - 1; i >= end_LHS_of_movable  && i >= 0; i--)
			{
				for (int j = start_top_of_movable; j <= start_bottom_of_movable; j++)
				{
					if( _screen_map[i][j] == SAND || _screen_map[i][j] == HOLE)
					{					
						_screen_map[i][j] = DIGDUG;
						_screen_map[i + length_of_movable ][j] = HOLE;
						movable.set_location(i + movable.get_x_boundary(), start_movable_center_y);
					}

					//else if( _screen_map[i][j] == HOLE)
					//{					
					//	_screen_map[i][j] = DIGDUG;
					//	_screen_map[i][j + height_of_movable +1] = HOLE;
					//}

					else if(_screen_map[i][j] == MONSTER)
					{
						movable.set_status(DEAD);
						final_move_status = DIED_TRYING_TO_MOVE;
					}
				}
			}
}
		
void Game::move_digdug_right( Digdug &movable, Move_status &final_move_status)
{

	// return the x-location of the center of the object
	int start_movable_center_x = movable.get_x_location(); 
// return the y-location of the center of the object
	int start_movable_center_y = movable.get_y_location();
// get the coordinates and dimensions of the movable
	int start_top_of_movable = start_movable_center_y - movable.get_y_boundary();
	int start_bottom_of_movable = start_movable_center_y + movable.get_y_boundary();
	int start_LHS_of_movable = start_movable_center_x - movable.get_x_boundary();
	int start_RHS_of_movable = start_movable_center_x + movable.get_x_boundary();
	int length_of_movable = 2*movable.get_x_boundary() + 1;
	int speed_of_movable = movable.get_speed();

	int end_RHS_of_movable = start_RHS_of_movable + speed_of_movable;

	for (int i = start_RHS_of_movable + 1; i <= end_RHS_of_movable  && i < _game_grid_elements; i++)
			{
				for (int j = start_top_of_movable; j <= start_bottom_of_movable; j++)
				{
					if( _screen_map[i][j] == SAND || _screen_map[i][j] == HOLE)
					{					
						_screen_map[i][j] = DIGDUG;
						_screen_map[i - length_of_movable][j] = HOLE;
						movable.set_location(i - movable.get_x_boundary(), start_movable_center_y);
					}

					//else if( _screen_map[i][j] == HOLE)
					//{					
					//	_screen_map[i][j] = DIGDUG;
					//	_screen_map[i][j + height_of_movable +1] = HOLE;
					//}

					else if(_screen_map[i][j] == MONSTER)
					{
						movable.set_status(DEAD);
						final_move_status = DIED_TRYING_TO_MOVE;
					}
				}
			}
}

	
	
	
Move_status Game::move_Digdug(Digdug &movable, const Direction &direction)
{
	Move_status final_move_status = CANNOT_MOVE;

	movable.set_direction(direction);

	int length_of_movable = 2*movable.get_x_boundary() + 1;
		// return the x-location of the center of the object
	int start_movable_center_x = movable.get_x_location(); 
// return the y-location of the center of the object
	int start_movable_center_y = movable.get_y_location();
// get the coordinates and dimensions of the movable
	int movable_type = movable.get_object_type();


	if (movable_type == DIGDUG)
	{
		switch(direction)
		{
		case UP:
			move_digdug_up(movable, final_move_status);
			break;
		case DOWN:
			move_digdug_down(movable, final_move_status);
			break;
		case LEFT:
			move_digdug_left(movable, final_move_status);
			break;
		case RIGHT:
			move_digdug_right(movable, final_move_status);
			break;
		}

	}
			
	if(final_move_status == DIED_TRYING_TO_MOVE)
	{
		return final_move_status;
	}

	int final_movable_center_x = movable.get_x_location();
	int final_movable_center_y = movable.get_y_location();
				
	//movable.set_location(final_movable_center_x, final_movable_center_y);

	if(final_movable_center_x == start_movable_center_x  && final_movable_center_y == start_movable_center_y)
	{
		final_move_status = CANNOT_MOVE;
	}
	else
	{
		final_move_status = CAN_MOVE;
	}
				
	return final_move_status;
}